package com.example.team6.controller;




import com.example.team6.entity.Course;
import com.example.team6.mapper.CourseMapper;
import com.example.team6.mapper.WatchstatusMapper;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.web.bind.annotation.RestController;
import com.example.team6.entity.Sc;
import com.example.team6.entity.resultt;
import com.example.team6.mapper.ScMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team6
 * @since 2020-06-30
 */
@RestController
//@RequestMapping("/team6/sc")
@CrossOrigin
public class ScController {
    @Autowired
    ScMapper scMapper;

    @Autowired
    CourseMapper courseMapper;

    @PostMapping(value = "api/aplication2")
    @ResponseBody
    public List<Sc> adaplication (@RequestBody Sc requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("studentid", requestUser.getStudentid());
        List<Sc> courseList = scMapper.selectByMap(map);

        return courseList;
    }

    @PostMapping(value = "api/getcount")
    @ResponseBody
    public List<Double> getcount (@RequestBody Sc requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("studentid", requestUser.getStudentid());
        List<Sc> courseList = scMapper.selectByMap(map);
        List<Double> countlist = new ArrayList<>();

        for (Sc sclist : courseList){
            Course course = new Course();
            Course courses= new Course();
            course.setCourseid(sclist.getCourseid());
            courses = courseMapper.selectById(course);
            System.out.println(sclist.getVideocount());
            System.out.println(courses.getMaincount());
            double count;
            if (sclist.getVideocount() != null && courses.getMaincount() != null) {
                count = (double) sclist.getVideocount() / courses.getMaincount();
            }
            else
                count = 0;
            countlist.add(count);
            System.out.println(count);
        }

        return countlist;
    }

    @PostMapping(value = "api/isadd")
    @ResponseBody
    public List<Sc> isadd (@RequestBody Sc requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("studentid", requestUser.getStudentid());
        map.put("courseid",requestUser.getCourseid());
        List<Sc> courseList = scMapper.selectByMap(map);

        return courseList;
    }

    @PostMapping(value = "api/toadd")
    @ResponseBody
    public resultt sturegister (@RequestBody Sc requestUser) {
        resultt resultt = new resultt();

        System.out.println(requestUser.getColloge() + ":" + requestUser.getCourseName() + ":" + requestUser.getCourseTeacher());

        Sc sc = new Sc();
        sc.setCourseid(requestUser.getCourseid());
        sc.setStudentid(requestUser.getStudentid());
        sc.setTeacherid(requestUser.getTeacherid());
        sc.setColloge(requestUser.getColloge());
        sc.setCourseName(requestUser.getCourseName());
        sc.setCourseTeacher(requestUser.getCourseTeacher());
        sc.setImg(requestUser.getImg());

        Integer result = scMapper.insert(sc);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("scid", sc.getScid());
            resultt.setData(map);
        }

        return resultt;
    }


    @PostMapping(value = "api/stucoursed")
    @ResponseBody
    public List<Sc> stucoursed (@RequestBody Sc requestUser) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("studentid", requestUser.getStudentid());
        List<Sc> scList = scMapper.selectByMap(map);

        return scList;
    }


}

